<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$section_id = mysql_real_escape_string(strip_tags($_GET['sec_id']));
$query = "SELECT *, csm.id AS csm_id, c.id AS cid, csub.id AS sid
 		  FROM course_sections cs
		  JOIN course_section_mapping csm ON csm.course_section_id = cs.id
		  JOIN courses c ON c.id = csm.course_id
		  JOIN course_subjects csub ON csub.id = c.course_subject_id
		  WHERE cs.id = '$section_id'";
$result = mysql_query($query);
$row=mysql_fetch_assoc($result);
$csm_id = $row['csm_id'];
$course_id = $row['cid'];
$subject_id = $row['sid'];
$subject_name = $row['subject_title'];
$abbreviation = $row['abbreviation'];
$course_number = $row['course_number'];
$course_name = $row['title'];

$orig_section_number = $row['section_number'];
$orig_semester_id = $row['semester_id'];
$orig_instructor_id = $row['instructor_id'];
$orig_room_type_id = $row['classroom_type_id'];
$orig_max_students = $row['num_seats'];
$orig_honors = $row['honors_program'];

echo "<p>";
	echo "<a href='index.php?sid=$subject_id&id=$course_id'>&lt;&lt; Back to Sections Portal</a><br />";
echo "</p>";
echo "<h1>Edit Section for $abbreviation $course_number</h1>";

//counting variable
$i = 0;

$query="SELECT * FROM instructors";
$result=mysql_query($query);

//determine dynamically number of rows read from table
$num_rows=mysql_numrows($result);

echo '<p>';

echo "<form action='edit_do.php' method='post'>";

//start table
echo "<table border='0'>";
	
	//create table row
	echo "<tr>";
		echo "<td><b>Section Number: </b></td>";
		echo "<td><input type='text' name='section_num' value='$orig_section_number'></td>";
	echo "</tr>";
	
	//create table row
	echo "<tr>";
		echo "<td><b>Instructor: </b></td>";
		echo "<td>";
			echo "<select name='instructor'>";
				while ($i < $num_rows)
				{
					$instructor_id=mysql_result($result,$i,"id");
					$first_name=mysql_result($result,$i,"first_name");
					$last_name=mysql_result($result,$i,"last_name");
					
					if ($orig_instructor_id == $instructor_id)
					{
						echo "<option value=$instructor_id selected='selected'>$last_name, $first_name</option>";
					}
					else
					{
						echo "<option value=$instructor_id>$last_name, $first_name</option>";
					}
					
					$i++;
				}
			echo "</select>";	
		echo "</td>";	
	echo "</tr>";
	
	$i=0;
	
	$get_room_type=mysql_query("SELECT * FROM classroom_types");
	$num_types=mysql_numrows($get_room_type);
	
	echo "<tr>";
		echo "<td><b>Room Type: </b></td>";
		echo "<td>";
			echo "<select name='room_type'>";
				while ($i < $num_types)
				{
					$type_description=mysql_result($get_room_type,$i,"description");
					$type_id=mysql_result($get_room_type,$i,"id");
					
					if ($orig_room_type_id == $type_id)
					{
						echo "<option value=$type_id selected='selected'>$type_description</option>";
					}
					else
					{
						
						echo "<option value=$type_id>$type_description</option>";
					}
					
					$i++;
				}
			echo "</select>";	
		echo "</td>";	
	echo "</tr>";
	
	//create table row
	echo "<tr>";
		echo "<td><b>Max Students: </b></td>";
		echo "<td><input type='text' name='max_s' value='$orig_max_students'></td>";
	echo "</tr>";
	
	//create table row
	echo "<tr>";
		echo "<td><b>Honors: </b></td>";
		echo '<td>';
			echo "<select name='honors'>";
				if ($orig_honors)
				{
					echo "<option value=1 selected='selected'>Yes</option>";
					echo "<option value=0>No</option>";
				}
				else
				{
					echo "<option value=1>Yes</option>";
					echo "<option value=0 selected='selected'>No</option>";
				}
				
			echo "</select>";
		echo '</td>';
	echo "</tr>";
	
	$get_semesters = mysql_query("
		SELECT *
		FROM seasons
		JOIN semesters
		ON semesters.season_id=seasons.id
		ORDER BY seasons.season, semesters.year");
	
	$num_semesters = mysql_numrows($get_semesters);
	
	$i=0;
	
	echo "<tr>";
		echo "<td><b>Semester: </b></td>";
		echo "<td>";
			echo "<select name='semester'>";
				while ($i < $num_semesters)
				{
					$semester_id=mysql_result($get_semesters,$i,"semesters.id");
					$year=mysql_result($get_semesters,$i,"semesters.year");
					$season=mysql_result($get_semesters,$i,"seasons.season");
					
					if ($orig_semester_id == $semester_id)
					{
						echo "<option value=$semester_id selected='selected'>$season $year</option>";
					}
					else
					{
						echo "<option value=$semester_id>$season $year</option>";
					}
					
					$i++;
				}
			echo "</select>";	
		echo "</td>";	
	echo "</tr>";
	
//end table tag
echo "</table>";

//close
mysql_close();

echo "<input type='hidden' name='sec_id' value='$section_id'>";
echo "<input type='hidden' name='csm_id' value='$csm_id'>";
echo "<input type='hidden' name='cid' value='$course_id'>";
echo "<input type='hidden' name='sid' value='$subject_id'>";
echo "<p><input type='submit'></p>";
echo '</form>';



?>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>